package cn.iocoder.yudao.module.system.controller.admin.passengerinfo;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.passengerinfo.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.passengerinfo.PassengerInfoDO;
import cn.iocoder.yudao.module.system.service.passengerinfo.PassengerInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 乘客信息")
@RestController("adminPassengerInfoController")
@RequestMapping("/system/passenger-info")
@Validated
public class PassengerInfoController {

    @Resource
    private PassengerInfoService passengerInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建乘客信息")
    @PreAuthorize("@ss.hasPermission('system:passenger-info:create')")
    public CommonResult<Long> createPassengerInfo(@Valid @RequestBody PassengerInfoCreateReqVO createReqVO) {
        return success(passengerInfoService.createPassengerInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新乘客信息")
    @PreAuthorize("@ss.hasPermission('system:passenger-info:update')")
    public CommonResult<Boolean> updatePassengerInfo(@Valid @RequestBody PassengerInfoUpdateReqVO updateReqVO) {
        passengerInfoService.updatePassengerInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除乘客信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:passenger-info:delete')")
    public CommonResult<Boolean> deletePassengerInfo(@RequestParam("id") Long id) {
        passengerInfoService.deletePassengerInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得乘客信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:passenger-info:query')")
    public CommonResult<PassengerInfoRespVO> getPassengerInfo(@RequestParam("id") Long id) {
        PassengerInfoDO passengerInfo = passengerInfoService.getPassengerInfo(id);
        return success(BeanUtils.toBean(passengerInfo, PassengerInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得乘客信息分页")
    @PreAuthorize("@ss.hasPermission('system:passenger-info:query')")
    public CommonResult<PageResult<PassengerInfoRespVO>> getPassengerInfoPage(@Valid PassengerInfoPageReqVO pageReqVO) {
        PageResult<PassengerInfoDO> pageResult = passengerInfoService.getPassengerInfoPage(pageReqVO);
        
        // 调试：打印原始DO对象的idCard值
        for (PassengerInfoDO item : pageResult.getList()) {
            System.out.println("DEBUG - PassengerInfoDO userId: " + item.getUserId() + ", idCard: " + item.getIdCard());
        }
        
        PageResult<PassengerInfoRespVO> result = BeanUtils.toBean(pageResult, PassengerInfoRespVO.class);
        
        // 调试：打印转换后VO对象的idCard值
        for (PassengerInfoRespVO item : result.getList()) {
            System.out.println("DEBUG - PassengerInfoRespVO userId: " + item.getUserId() + ", idCard: " + item.getIdCard());
        }
        
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出乘客信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:passenger-info:export')")
    public void exportPassengerInfoExcel(@Valid PassengerInfoPageReqVO pageReqVO,
            HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PassengerInfoDO> list = passengerInfoService.getPassengerInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "乘客信息.xls", "数据", PassengerInfoExcelVO.class,
                BeanUtils.toBean(list, PassengerInfoExcelVO.class));
    }

    @GetMapping("/get-by-user-id")
    @Operation(summary = "根据用户ID获得乘客信息")
    @Parameter(name = "userId", description = "用户ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:passenger-info:query')")
    public CommonResult<PassengerInfoRespVO> getPassengerInfoByUserId(@RequestParam("userId") Long userId) {
        PassengerInfoDO passengerInfo = passengerInfoService.getPassengerInfoByUserId(userId);
        return success(BeanUtils.toBean(passengerInfo, PassengerInfoRespVO.class));
    }

}
